$(function(){
	var user = {
		onCorrect: function(el){
			el.closest(".control-group").removeClass("error");
			el.next().addClass('successtip').text('');
		},
		
		updateMsg : function(msg, el){
			if(el){
				el.closest(".control-group").addClass("error");
				el.next().removeClass('successtip').text(msg);
			}
		},
		
		init : function(){
			var self = this;
			function charMode(iN) {
                if (iN >= 48 && iN <= 57) //数字    
                    return 1;
                if (iN >= 65 && iN <= 90) //大写字母    
                    return 2;
                if (iN >= 97 && iN <= 122) //小写字母    
                    return 4;
                else return 8; //特殊字符    
            }

            function bitTotal(num) {
                var modes = 0;
                for (i = 0; i < 4; i++) {
                    if (num & 1) modes++;
                    num >>>= 1;
                }
                return modes;
            }

			$("#glodon_password").blur(function(){
				var t = $(this), 
				v = t.val();
		    	if(v === ''){
		    		self.updateMsg('请输入密码', t);
				}else if(v.length < 6 || v.length > 16){
					self.updateMsg('请输入正确的密码', t);
				}else{
		    		self.onCorrect(t);
				}
			}).keyup(function(){
				var passwordComplex = 0;
                var value = $(this).val();
                if (value.length == 0) {
                    passwordComplex = 0;
                } else {
                    var modes = 0;
                    for (i = 0; i < value.length; i++) {
                        var c = charMode(value.charCodeAt(i));
                        modes |= c;
                    }
                    passwordComplex = bitTotal(modes);
                }
                $('.password-power span').attr('class', 'empty');
                if (passwordComplex == 0) {
                    return;
                }
                $('.password-power span').each(function (index) {
                    var i = index + 1;
                    $(this).attr('class', 'good');
                    if (i == passwordComplex) {
                        return false;
                    }
                });
			});
			
			$("#glodon_password_confirm").blur(function(){
				var t = $(this), 
				v = $(this).val();
				if(v === ''){
					self.updateMsg('请重复输入密码', t);
				}else if( $("#glodon_password").val() !== $(this).val() ){
		    		self.updateMsg('两次输入的密码不一致', t);
				}else if(v.length >= 6 && v.length <= 16){
					self.onCorrect(t);
				}
			});
			
			$("#forPWD button[type='submit']").click(function(e){
			    var canSubmit = true;
			    $("#glodon_password").trigger('blur');
			    $("#glodon_password_confirm").trigger('blur');
				canSubmit = $('#forPWD .successtip').length === 2;
			    if( canSubmit ){
			        $("#mask").show();
			        $("#forPWD form").ajaxSubmit({
        				url:"/account/changePassword.json",
        				type:"post",
        				success : function(data){
        				    $("#mask").hide();
        				    if(eval(data.result)){
            				    $("#forPWD").hide(); 
            				    $("#sucPWD").show();
            				    (function jump(count) {    
                                    window.setTimeout(function(){    
                                        count--;    
                                        if(count > 0) {    
                                            $('#count').text( count );    
                                            jump(count);    
                                        } else {    
                                            location.href=window._global["contextPath"];    
                                        }    
                                    }, 1000);    
                                })(parseInt($("#count").text()));   
        				    }else{
        				        $("#forPWD").hide(); 
        				        $("#failPWD").show();
        				        (function jump(count) {    
                                    window.setTimeout(function(){    
                                        count--;    
                                        if(count > 0) {    
                                            $('#fcount').text( count );    
                                            jump(count);    
                                        } else {
                                            location.href=window._global["contextPath"];    
                                        }
                                    }, 1000);
                                })(parseInt($("#fcount").text()));   
        				    }
        				}
        			});
			    }
			    return false;
			});
		}
	}
	user.init();
})
